package MyStack;

import java.util.Arrays;

public class MyStack {
	private int [] elem;
	private int usedSize;
	private static final int DEFAULT_CAPACITY=10;

	public MyStack() {
		this.elem = new int [DEFAULT_CAPACITY];
	}
	public void display() {
		while(!isEmpty()) {
			int top=peek();
			System.out.print(top +" ");
			top();
		}
		System.out.println();
	}
	public void push(int val) {
		if(isFull()) {
			this.elem= Arrays.copyOf(this.elem,this.elem.length*2);
		}
		this.elem[usedSize]=val;
		usedSize++;
	}
	public boolean isFull() {
		return this.usedSize==this.elem.length;
	}
	//将栈顶元素删除出去
	public int top() {
		if(isEmpty()) {
			throw new EmptyException("栈里元素为空无法删除");
		}
		int top=this.elem[usedSize-1];
		usedSize--;
		return top;
	}
	public int peek() {
		if(isEmpty()) {
			throw new EmptyException("栈里元素为空无法删除");
		}
		return this.elem[usedSize-1];
	}
	public boolean isEmpty() {
		return usedSize==0;
	}
}
